package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.template.DestinationTemplate;
import cn.wolfcode.luowowo.search.template.StrategyTemplate;
import cn.wolfcode.luowowo.search.template.TravelTemplate;
import cn.wolfcode.luowowo.search.template.UserInfoTemplate;
import cn.wolfcode.luowowo.search.vo.SearchResultVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class SearchController {
    @Reference
    private IDestinationSearchService destinationSearchService;
    @Reference
    private ITravelSearchService travelSearchService;
    @Reference
    private IUserInfoSearchService userInfoSearchService;
    @Reference
    private IStrategySearchService strategySearchService;
    @Reference
    private ISearchService searchService;

    //搜索
    @RequestMapping("/q")  //q?type=-1&keyword=11
    public String query(Model model, @ModelAttribute("qo") SearchQueryObject qo){
        //搜索条件type   -1全部/ 0目的地/ 1攻略/ 2游记/ 3找人
        switch(qo.getType()){
            case 0: return destSearch(model, qo);       //目的地搜索
            case 1: return strategySearch(model, qo);   //攻略搜索
            case 2: return travelSearch(model, qo);     //游记搜索
            case 3: return userSearch(model, qo);       //用户搜索
            default: return allSearch(model, qo);       //搜索全部
        }
    }

    //目的地搜索
    private String destSearch(Model model, SearchQueryObject qo) {
        //dest
        DestinationTemplate dest = destinationSearchService.queryDestByName(qo.getKeyword());
        model.addAttribute("dest", dest);
        if(dest != null){  //如果存在, 查询地区下的游记/用户/攻略
            qo.setDestId(dest.getId());
            qo.setDestNeme(dest.getName());
            qo.setPageSize(5);
            //data.strategys
            Page<StrategyTemplate> strategys =  strategySearchService.queryStrategyByDestId(qo);
            //data.users
            Page<UserInfoTemplate> users =  userInfoSearchService.queryUserInfoByDestId(qo);
            //data.travels
            Page<TravelTemplate> travels =  travelSearchService.queryTravelByDestId(qo);

            //model.addAttribute("data", vo);
            SearchResultVO vo = new SearchResultVO();
            vo.setStrategys(strategys.getContent());
            vo.setTravels(travels.getContent());
            vo.setUsers(users.getContent());
            vo.setTotal(strategys.getTotalElements() + travels.getTotalElements() + users.getTotalElements());
            model.addAttribute("data", vo);
        }
        //如果查询不到地区, 提示查询条数为0
        return "index/searchDest";
    }
    //攻略搜索
    private String strategySearch(Model model, SearchQueryObject qo) {
        //page
        Page<StrategyTemplate> strategys = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME
                , StrategyTemplate.TYPE_NAME, StrategyTemplate.class, qo, "title", "subTitle", "summary");

        model.addAttribute("page", strategys);
        return "index/searchStrategy";
    }
    //游记搜索
    private String travelSearch(Model model, SearchQueryObject qo) {
        //page
        Page<TravelTemplate> travels = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME
                , TravelTemplate.TYPE_NAME, TravelTemplate.class, qo, "title", "summary");

        model.addAttribute("page", travels);
        return "index/searchTravel";
    }
    //用户搜索
    private String userSearch(Model model, SearchQueryObject qo) {
        //page
        Page<UserInfoTemplate> users = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME
                , UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class, qo, "nickname", "destName");
        model.addAttribute("page", users);
        return "index/searchUser";
    }

    //搜索全部
    private String allSearch(Model model, SearchQueryObject qo) {
        //data.dest
        Page<DestinationTemplate> dests = searchService.searchWithHighlight(DestinationTemplate.INDEX_NAME
                , DestinationTemplate.TYPE_NAME, DestinationTemplate.class, qo, "name", "info");
        //data.strategys
        Page<StrategyTemplate> strategys = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME
                , StrategyTemplate.TYPE_NAME, StrategyTemplate.class, qo, "title", "subTitle", "summary");
        //data.travels
        Page<TravelTemplate> travels = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME
                , TravelTemplate.TYPE_NAME, TravelTemplate.class, qo, "title", "summary");
        //data.users
        Page<UserInfoTemplate> users = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME
                , UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class, qo, "nickname", "destName");

        SearchResultVO vo = new SearchResultVO();
        vo.setUsers(users.getContent());
        vo.setDests(dests.getContent());
        vo.setTravels(travels.getContent());
        vo.setStrategys(strategys.getContent());;
        //data.total
        vo.setTotal(users.getTotalElements()+strategys.getTotalElements()+dests.getTotalElements()+travels.getTotalElements());
        model.addAttribute("data", vo);
        return "index/searchAll";
    }
}
